﻿
using System;
using System.Collections.Generic;
using System.Data;
using QLKS.Entity;

namespace QLKS.DAL
{
    public class DMLoaiKhachHangDAO
    {
        private SqlDataProvider dataProvider;

        public DMLoaiKhachHangDAO()
        {
            dataProvider = new SqlDataProvider();
        }

        public List<DMLoaiKhachHang> GetByAll(DMLoaiKhachHang obj)
        {
            List<DMLoaiKhachHang> lstData = new List<DMLoaiKhachHang>(); ;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_DMLoaiKhachHang_GetByAll"
                    , new string[] { "MaLoaiKhachHang", "TenLoaiKhachHang", "HeSo", "GhiChu" }
                    , new object[] { obj.MaLoaiKhachHang, obj.TenLoaiKhachHang, obj.HeSo, obj.GhiChu }
                );
                DMLoaiKhachHang entity;
                foreach(DataRow myrow in dt.Rows)
                {
                    entity = new DMLoaiKhachHang();
                    entity.MaLoaiKhachHang = Convert.ToInt32(myrow["MaLoaiKhachHang"]);
                    entity.TenLoaiKhachHang = myrow["TenLoaiKhachHang"] is DBNull ? string.Empty : myrow["TenLoaiKhachHang"].ToString();
                    entity.HeSo = Convert.ToDouble(myrow["HeSo"]);
                    entity.GhiChu = myrow["GhiChu"] is DBNull ? string.Empty : myrow["GhiChu"].ToString();
                    lstData.Add(entity);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }

        public bool Insert(DMLoaiKhachHang obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMLoaiKhachHang_Insert"
                    , new string[] { "MaLoaiKhachHang", "TenLoaiKhachHang", "HeSo", "GhiChu" }
                    , new object[] { obj.MaLoaiKhachHang, obj.TenLoaiKhachHang, obj.HeSo, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }

        public bool Update(DMLoaiKhachHang obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMLoaiKhachHang_Update"
                    , new string[] { "MaLoaiKhachHang", "TenLoaiKhachHang", "HeSo", "GhiChu" }
                    , new object[] { obj.MaLoaiKhachHang, obj.TenLoaiKhachHang, obj.HeSo, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }

        public bool Delete(DMLoaiKhachHang obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMLoaiKhachHang_Delete"
                    , new string[] { "MaLoaiKhachHang", "TenLoaiKhachHang", "HeSo", "GhiChu" }
                    , new object[] { obj.MaLoaiKhachHang, obj.TenLoaiKhachHang, obj.HeSo, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }
    }
}
